﻿#include "ReverseListBetween.h"

#ifdef DEV_TEST
#include <gtest/gtest.h>
class ReverseListBetweenTestCase
{
public:
	std::string linkList;
	//注意:0<m ≤n ≤size
	int m;
	int n;

	friend std::ostream& operator<<(std::ostream& os, const ReverseListBetweenTestCase& obj)
	{
		return os
			<< "linkList: " << obj.linkList
			<< " m: " << obj.m
			<< " n: " << obj.n;
	}
};

class ReverseListBetweenTest :public SolutionTestor<IReverseListBetween, testing::TestWithParam<ReverseListBetweenTestCase>>
{
protected:
	void LoadSolutions(std::vector<IReverseListBetween*>& solutions) override
	{
		solutions = {
			new ReverseListBetween::Solution,
		};
	}
};
TEST_P(ReverseListBetweenTest, reverseListBetween)
{
	TestForSolutions([](IReverseListBetween* solution)
		{
			const auto testCase = GetParam();
			//将链表的值转到数组中，在数组中做区间反转
			std::vector<int> arr;
			FormatedUtil::parseFormated(arr, testCase.linkList);
			std::reverse(arr.begin() + testCase.m - 1, arr.begin() + testCase.n);

			//获取区间反转后的链表
			ListNode listHead;
			listHead.parseFormated(testCase.linkList);
			ListNode* reversedListHead = solution->reverseListBetween(&listHead, testCase.m, testCase.n);

			//与期望的反转结果比较
			const std::string expectResult = FormatedUtil::dumpFormated(arr);
			const std::string result = reversedListHead->dumpFormated();
			EXPECT_EQ(result, expectResult);
		});
}

INSTANTIATE_TEST_SUITE_P(Validity, ReverseListBetweenTest, testing::Values(
	ReverseListBetweenTestCase{ "[1,2,3,4,5]",2,4 },
	ReverseListBetweenTestCase{ "[1,2,3,4,5]",1,4 },
	ReverseListBetweenTestCase{ "[1,2,3,4,5]",2,5 },
	ReverseListBetweenTestCase{ "[1,2,3,4,5]",1,5 }
));
#endif